// pages/zhanbo/index.js
const app = getApp();
const http = require('../../utils/http.js');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		HTTP: http.ipRoot,
		bottomHeight: app.globalData.model.bottomHeight,
		imgUrl: '',
		videoUrl: '',
		typeList: [],
		typeIndex: 0,
		typeValue: '类型',
		dqList: [{
				value: '全部',
				icon: '../../images/p_all.png',
				selectIcon: '../../images/p_all2.png'
			},
			{
				value: '华北',
				icon: '../../images/p_hb.png',
				selectIcon: '../../images/p_hb2.png'
			},
			{
				value: '华东',
				icon: '../../images/p_hd.png',
				selectIcon: '../../images/p_hd2.png'
			},
			{
				value: '华中',
				icon: '../../images/p_hz.png',
				selectIcon: '../../images/p_hz2.png'
			},
			{
				value: '西北',
				icon: '../../images/p_xb.png',
				selectIcon: '../../images/p_xb2.png'
			},
			{
				value: '西南',
				icon: '../../images/p_xn.png',
				selectIcon: '../../images/p_xn2.png'
			},
			{
				value: '华南',
				icon: '../../images/p_hn.png',
				selectIcon: '../../images/p_hn2.png'
			},
			{
				value: '东北',
				icon: '../../images/p_db.png',
				selectIcon: '../../images/p_db2.png'
			}
		],
		dqType: '全部',
		inputValue: '',
		list: [],

		isLoading: true,
		isnoMore: false,
		pageCurrent: 1,
		vote_num: 0
	},
	getType() {
		var vote_num = wx.getStorageSync('userInfo').use_vote_num;
		var typeList = [{
			key: 0,
			value: '类型'
		}]
		http.post('api/other/enum').then(res => {
			Object.entries(res.data.wk_cate_enum).map(([key, value]) => {
				var obj = {
					'key': key,
					'value': value
				};
				typeList.push(obj);
			});
			this.setData({
				typeList: typeList,
				vote_num
			})

		}).then(() => {
			http.post('api/advert/get_advert', {
				en: 'ad_wk_video'
			}).then((res) => {
				this.setData({
					videoUrl: res.data[0].videourl,
					imgUrl: res.data[0].imgurl
				})
			})
		})
	},
	getList(isFirst) {
		var that = this;
		var data = {
			reg: that.data.dqType,
			i: that.data.typeIndex,
			t: this.data.inputValue,
			pg: isFirst ? 1 : this.data.pageCurrent
		}
		http.post('api/works/list_region', data, 1).then(res => {
			var pageCurrent = that.data.pageCurrent;
			that.setData({
				pageCurrent: isFirst ? 2 : pageCurrent + 1,
				list: isFirst ? res.data : that.data.list.concat(res.data),
				isLoading: false,
				isListMore: true
			})
			if (res.data.length < 1) {
				setTimeout(function() {
					that.setData({
						isListMore: false,
						isLoading: false,
						isShowResultView: true,
						isnoMore: isFirst ? false : true
					})
				}, 10)

			}
		})
	},
	changeDq(e) {
		var value = e.currentTarget.dataset.value;
		this.setData({
			dqType: value
		})
		this.getList(1);
	},
	goPath(e) {
		var url = e.currentTarget.dataset.url;
		wx.navigateTo({
			url,
		})
	},
	bindTypeChange(e) {
		this.setData({
			typeIndex: this.data.typeList[e.detail.value].key,
			typeValue: this.data.typeList[e.detail.value].value
		})
		this.getList(1)
	},
	onInput(e) {
		this.setData({
			inputValue: e.detail
		})
	},
	onSearch: app.debounce(function() {
		this.getList(1);
	}, 500),

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {

	},
	onBack() {
		wx.navigateBack();
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		this.getType();
		this.getList(1);
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {
		// 刷新逻辑，例如请求数据并更新页面
		wx.showLoading({
		    title: '加载中..',
		    mask: true
		})
		setTimeout(() => {
			wx.hideLoading();
			// 更新数据
			this.onShow();
			// 停止下拉刷新
			wx.stopPullDownRefresh();
		}, 2000);
		
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {
		if (this.data.isListMore) {
			this.setData({
				isLoading: true
			})
			this.getList();
		}
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})